import 'package:flutter/material.dart';
import 'package:smart_health_app/pages/login_page.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'home1_3.dart';
import 'home1_2.dart';
import 'home1_1.dart';

class Home0Page extends StatelessWidget {
  const Home0Page({Key? key}) : super(key: key);

  Future<bool> _checkLoginStatus() async {
    final prefs = await SharedPreferences.getInstance();
    return prefs.getBool('isLoggedIn') ?? false;
  }

  Future<void> _handleNavigation(BuildContext context, String route) async {
    final isLoggedIn = await _checkLoginStatus();
    if (isLoggedIn) {
      Navigator.pushNamed(context, route);
    } else {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => LoginPage(nextRoute: route),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              //colors: [Color(0xFF12B2B3), Color(0xFF1DC4A0)],
              colors: [Color(0xFF1DC4A0), Color(0xFF1DC4A0)]),
        ),
        child: SafeArea(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // 欢迎文本
              Padding(
                padding: const EdgeInsets.fromLTRB(20, 40, 20, 40),
                child: Text(
                  '欢迎使用联康智护app，\n请选择模块',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 24,
                    fontFamily: 'Mulish',
                    fontWeight: FontWeight.w700,
                    height: 1.3,
                  ),
                ),
              ),

              // 选项卡片区域
              Expanded(
                child: Container(
                  width: double.infinity,
                  padding: const EdgeInsets.fromLTRB(20, 20, 20, 20),
                  decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(30),
                      topRight: Radius.circular(30),
                    ),
                  ),
                  child: Center(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        _buildOptionCard(
                          title: '大数据协同训练层',
                          color: const Color(0xFF0065FF),
                          iconPath: 'assets/icons/company.png',
                          onTap: () => _handleNavigation(context, '/home1_1'),
                        ),
                        const SizedBox(height: 40),
                        _buildOptionCard(
                          title: '医生',
                          color: const Color(0xFFFEAA48),
                          iconPath: 'assets/icons/doctor_home0.png',
                          onTap: () => _handleNavigation(context, '/home1_2'),
                        ),
                        const SizedBox(height: 40),
                        _buildOptionCard(
                          title: '普通用户',
                          color: const Color(0xFFFD586B),
                          iconPath: 'assets/icons/patient_home0.png',
                          onTap: () => _handleNavigation(context, '/home1_3'),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildOptionCard({
    required String title,
    required Color color,
    required String iconPath,
    required VoidCallback onTap,
  }) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        onTap: onTap,
        borderRadius: BorderRadius.circular(15),
        child: Container(
          width: double.infinity,
          height: 100, // 增加卡片高度
          decoration: BoxDecoration(
            color: color,
            borderRadius: BorderRadius.circular(15),
            boxShadow: [
              BoxShadow(
                color: color.withOpacity(0.3),
                blurRadius: 8,
                offset: const Offset(0, 4),
              ),
            ],
          ),
          child: Row(
            children: [
              // 图标
              Padding(
                padding: const EdgeInsets.only(left: 35),
                child: Image.asset(
                  iconPath,
                  width: 40,
                  height: 40,
                ),
              ),
              const SizedBox(width: 20),
              // 文本
              Text(
                title,
                style: const TextStyle(
                  color: Colors.white,
                  fontSize: 20,
                  fontFamily: 'Mulish',
                  fontWeight: FontWeight.w700,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
